package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.GoodsLimitBean;
import cn.xfdzx.android.apps.shop.bean.GoodsMiaoSBean;
import cn.xfdzx.android.apps.shop.contract.GoodsLimitContract;
import cn.xfdzx.android.apps.shop.model.GoodsLimitModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author zf
 * @date 2020/8/17.
 * Description：限量商品
 */
public class GoodsLimitPresenter extends BasePresenter<GoodsLimitContract.View> implements GoodsLimitContract.Presenter {

    private GoodsLimitContract.Model model;

    public GoodsLimitPresenter() {
        model = new GoodsLimitModel();
    }

    @Override
    public void getGoodsLimitList(boolean isRefresh, boolean isLoading, String limit, int page) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.getGoodsLimitList(limit,page)
                .compose(RxScheduler.<GoodsLimitBean>Flo_io_main())
                .as(mView.<GoodsLimitBean>bindAutoDispose())
                .subscribe(new Consumer<GoodsLimitBean>() {
                    @Override
                    public void accept(GoodsLimitBean bean) throws Exception {
                        mView.getGoodsLimitListSuccess(bean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                });

    }

    @Override
    public void getGoodsLimitListHighSales() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        //mView.showLoading();
        model.getGoodsLimitListHighSales()
                .compose(RxScheduler.<GoodsLimitBean>Flo_io_main())
                .as(mView.<GoodsLimitBean>bindAutoDispose())
                .subscribe(new Consumer<GoodsLimitBean>() {
                    @Override
                    public void accept(GoodsLimitBean bean) throws Exception {
                        mView.getGoodsLimitListHighSalesSuccess(bean);
                        //mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        //mView.hideLoading();
                    }
                });

    }

    @Override
    public void getGoodsMiaoShaList(boolean isRefresh, boolean isLoading, String limit, int page) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.getGoodsMiaoShaList(limit,page)
                .compose(RxScheduler.<GoodsMiaoSBean>Flo_io_main())
                .as(mView.<GoodsMiaoSBean>bindAutoDispose())
                .subscribe(new Consumer<GoodsMiaoSBean>() {
                    @Override
                    public void accept(GoodsMiaoSBean bean) throws Exception {
                        mView.getGoodsMiaoShaListSuccess(bean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }

    @Override
    public void getGoodsStock(String good_id) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getGoodsStock(good_id)
                .compose(RxScheduler.<GoodsMiaoSBean>Flo_io_main())
                .as(mView.<GoodsMiaoSBean>bindAutoDispose())
                .subscribe(new Consumer<GoodsMiaoSBean>() {
                    @Override
                    public void accept(GoodsMiaoSBean bean) throws Exception {
                        mView.getGoodsStockSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });

    }

    @Override
    public void getLimitGoodsStock(String good_id) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getLimitGoodsStock(good_id)
                .compose(RxScheduler.<GoodsMiaoSBean>Flo_io_main())
                .as(mView.<GoodsMiaoSBean>bindAutoDispose())
                .subscribe(new Consumer<GoodsMiaoSBean>() {
                    @Override
                    public void accept(GoodsMiaoSBean bean) throws Exception {
                        mView.getLimitGoodsStockSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });

    }
}
